<?php
namespace Common\Models;
use Common\BaseModel;
class UserMonthIncome  extends BaseModel
{
	public $site;
	public $redis_key="";
	public function initialize()
	{
		parent::initialize();
		$this->setSource('user_month_income');
	}



    //根据日期查找上个月用户是否已经结算了佣金
    public function find_month_user($date, $user_id)
    {
        $conditions        = "month ='{$date}' and user_id={$user_id}";
        $user_month_income = $this->findFirst(array(
            $conditions
        ));
        return $user_month_income;
    }


    public function add_log($data,$date){
	//	$wallet= new Wallets();
		foreach($data as $k =>$v){
			$condition['conditions']="month ='$date' and user_id=$k";
			$money=$this->findFirst($condition);
			$is_add=0;
			//之前有了
			if($money == false){
				$money=new UserMonthIncome();
				}
				$money->user_id=$k;
				$money->commission=$v['commission'];
				$money->market=$v['market'];
				$money->month=$date;
						$money->save();
			}
	//die('ww');	
		return true;
	}
	public function  get_income($user_id){
		$condition['conditions']="user_id=$user_id";
		$condition['columns']='commission,market,area,ad,red,new_people,month';
		return $this->find($condition)->toarray();

	}


    public function  get_income_data($user_id,$data=[]){
	    $sql_str="user_id=$user_id";
	    if(isset($data['month'])){
            $sql_str .= " AND month='{$data['month']}'";
        }
        $condition['conditions']=$sql_str;
        $condition['columns']='commission,market,area,ad,red,new_people,month';
        $condition['order']='month desc';
        $result = $this->findFirst($condition);
        if($result == false){
            return [];
        }
        return $result->toarray();
    }


	public function is_up_founder($user_id){
		$config=1000;
		$time= date('Y-m-d',strtotime('-180 day'));
		$condition['conditions']="user_id=$user_id and (commission+market) >= $config and month >= '$time'";
		$condition['columns']='id';
		$res=$this->findFirst($condition);
		if($res == false){
		return false;
		}
		return true;
	}
	public function get_all_income($user_id){
		$condition['conditions']="user_id=$user_id";
		$condition['columns']='sum(commission+market+area+ad+red+new_people+new_area_people) as money';
		$res=$this->findFirst($condition);
		if(!isset($res->money) || empty($res->money)){
			return "0.00";
		}
		return $res->money."";
	}
	//核账接口
	public function get_all_money(){
		$condition['columns']='sum(commission+market+area+ad+red+new_people+new_area_people) as money,user_id';
		$condition['group']='user_id';
		$res=$this->find($condition);
		return $res->toarray();
	}

	public function add_other_log($res,$date,$column){
		foreach($res as $k =>$v){
			$condition['conditions']="user_id= {$v['user_id']} and month='{$date}'";
			$info=$this->findFirst($condition);
			if($info == false){
				$info= new UserMonthIncome();
				$info->commission = 0;
				$info->market=0;
				$info->month=$date;
				$info->user_id=$v['user_id'];
			}
			$info->$column=$v['money'];
			$info->save();

		}
		die('complet');
	
	}


}
